<?php

/**
 * entries actions.
 *
 * @package    berta-s
 * @subpackage entries
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class entriesActions extends sfActions {
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
	public function executeIndex(sfWebRequest $request){
		$q=$this->getRoute()->getObjects();
		$this->forward404If(empty($q), "No such entries");

		$this->entries=$q->execute();

		$context=$this->getContext();
		$this->currentSection=null;
		if ($context->has("section")){
			$this->currentSection=$context->get("section");
		}

        $this->application=$context->getConfiguration()->getApplication();

		$this->currentTag=null;
		if ($context->has("tag")){
			$this->currentTag=$context->get("tag");
		}

        // in frontend:
        // if no tag is selected and there is tags for all entries in section
        // we must redirect to sections first tag's url
        if (sfConfig::get("sf_app")=="frontend" && sizeof($this->entries)>0 && $this->currentSection!="" && $this->currentTag==""){
            // check if there is tags in all entries
            $allTagged=true;
            $firstTag=null;
            $untaggedEntries=array();
            foreach ($this->entries as $entry){
                $tags=$entry->getSavedTagsQuery()->execute();
                /** @var Doctrine_Collection $tags **/
                if (sizeof($tags)<1){
                    $allTagged=false;
                    $untaggedEntries[]=$entry;
                }else if (empty($firstTag)){
                    $firstTag=$tags->getFirst();
                }
            }

            // all entries are tagged, we must redirect to first tag's page
            if ($allTagged){
                $context->getConfiguration()->loadHelpers("Url");
                $this->redirect(
                    url_for(
                        "entries_by_section_slug_and_tag_slug",
                        array(
                             "section_slug" => $this->currentSection->getSlug(),
                             "tag_slug"     => $firstTag->getSlug(),
                        )
                    )
                );
            }else{ // all entries not tagged, we must show only those untagged ones in section page
                $this->entries=$untaggedEntries;
            }
        }


		$this->settings=Settings::getInstance();
		/* @var $context sfContext */
		$context->set("settings", $this->settings);

		$this->templateSettings=TemplateSettings::getInstance();
		$context->set("templateSettings", $this->templateSettings);

		/** @TODO: move to separate process! similar to entry snippets **/
		if ($request->hasParameter("snippetName")){
            sfConfig::set('sf_web_debug', false);
			$this->snippetName=$request->getParameter("snippetName");
			$this->setTemplate("snippet");
		}

        
        /** @var $widgetsQuery Doctrine_Query */
        $widgetsQuery=
            Doctrine::getTable("WidgetPosition")
            ->createQuery()
            ->leftJoin("WidgetPosition.Widget")
        ;

        if ($this->currentSection!=""){
            $widgetsQuery->andWhere("section_id = ?", $this->currentSection->getId());
        }

        $widgetsQuery->orWhere("pinned = ?", true);
        $widgetsQuery->orderBy("WidgetPosition.position ASC");
        $widgets=$widgetsQuery->execute();

        $widgetPositionsGrouped=array();
        foreach ($widgets as $widget){
            $widgetPositionsGrouped[$widget->getPlaceholderName()][]=$widget;
        }

        $this->widgetPositionsGrouped=$widgetPositionsGrouped;
	}
}
